package tushare

import (
	"fmt"
	"strconv"
	"tushare/model"
	"tushare/model/tushare"

	"github.com/golang-module/carbon"
)

type ElgVol struct{}

/**
超大单占比
**/
func (ev ElgVol) Elg(args []string) {
	if args[0] == "onlyElg" {
		ev.onlyElg(args)
	} else if args[0] == "downThree" {
		ev.downThree()
	} else if args[0] == "downFive" {
		ev.downFive(args)
	} else if args[0] == "volByDate" {
		ev.VolByDate(args)
	} else if args[0] == "upThree" {
		ev.upThree(args)
	} else if args[0] == "month" {
		ev.month(args)
	} else if args[0] == "elgVol" {
		ev.elgVol(args)
	}
}

func (ev ElgVol) VolByDate(args []string) {
	stocks := model.StockList()

	for _, v := range stocks {
		elgvol := tushare.ElgVolByDate(v.TsCode, args[1], args[2])
		if elgvol.Jf > 0 {
			elg := tushare.Elg{}
			elg.TsCode = v.TsCode
			elg.TradeDateFrom = args[1]
			elg.TradeDateTo = args[2]
			elg.TotalBuyElgVol = elgvol.Buy
			elg.TotalSellElgVol = elgvol.Sell
			elg.BuySellJf = elgvol.Jf
			elg.SellElgVolProportion = elgvol.Cf
			elg.CreateDate = carbon.Now().ToDateString()
			elg.Days = 100
			tushare.ElgCreate(elg)
			fmt.Printf("%s %d %d %d %f\n", v.TsCode, elgvol.Buy, elgvol.Sell, elgvol.Jf, elgvol.Cf)
		}
	}
}

func (ev ElgVol) downFive(args []string) {
	stocks := model.StockList()

	for _, v := range stocks {
		daily := tushare.RaisingLimitDays(5, v.TsCode)
		if len(daily) < 5 {
			continue
		}
		if daily[0].PctChg < daily[1].PctChg && daily[1].PctChg < daily[2].PctChg && daily[2].PctChg < daily[3].PctChg && daily[3].PctChg < daily[4].PctChg {
			elgvol := tushare.ElgVol(v.TsCode, args[1], 5)
			if elgvol.Buy > elgvol.Sell {
				elg := tushare.Elg{}
				elg.TsCode = v.TsCode
				elg.TradeDateFrom = args[1]
				elg.TradeDateTo = args[1]
				elg.TotalBuyElgVol = elgvol.Buy
				elg.TotalSellElgVol = elgvol.Sell
				elg.BuySellJf = elgvol.Jf
				elg.SellElgVolProportion = elgvol.Cf
				elg.CreateDate = carbon.Now().ToDateString()
				elg.Days = 5
				tushare.ElgCreate(elg)
				fmt.Printf("%s %d %d %f\n", v.TsCode, elgvol.Buy, elgvol.Sell, elgvol.Cf)
			}
		}

	}
}

func (ev ElgVol) downThree() {
	stocks := model.StockList()

	for _, v := range stocks {
		daily := tushare.RaisingLimitDays(3, v.TsCode)
		if len(daily) < 3 {
			continue
		}
		if daily[0].PctChg > 0 || daily[1].PctChg > 0 || daily[2].PctChg > 0 {
			continue
		}

		elgvol := tushare.ElgVol(v.TsCode, "20211217", 3)
		if elgvol.Buy > elgvol.Sell {
			fmt.Printf("%s %d %d %f\n", v.TsCode, elgvol.Buy, elgvol.Sell, elgvol.Cf)
		}
	}
}

func (ev ElgVol) onlyElg(args []string) {
	stocks := model.StockList()

	for _, v := range stocks {
		elgvol := tushare.ElgVolByDate(v.TsCode, args[1], args[2])
		if elgvol.Buy > 0 && elgvol.Sell == 0 {
			fmt.Printf("%s %d %d %f\n", v.TsCode, elgvol.Buy, elgvol.Sell, elgvol.Cf)
		}
	}
}

func (ev ElgVol) upThree(args []string) {
	stocks := model.StockList()

	for _, v := range stocks {
		daily := tushare.RaisingLimitDays(5, v.TsCode)
		if len(daily) < 5 {
			continue
		}
		if daily[0].Close > daily[1].Close && daily[1].Close > daily[2].Close && daily[2].Close < daily[3].Close && daily[3].Close < daily[4].Close {
			elgvol := tushare.ElgVol(v.TsCode, args[1], 3)
			if elgvol.Jf > 0 {
				elg := tushare.Elg{}
				elg.TsCode = v.TsCode
				elg.TradeDateFrom = args[1]
				elg.TradeDateTo = args[1]
				elg.TotalBuyElgVol = elgvol.Buy
				elg.TotalSellElgVol = elgvol.Sell
				elg.BuySellJf = elgvol.Jf
				elg.SellElgVolProportion = elgvol.Cf
				elg.CreateDate = carbon.Now().ToDateString()
				elg.Days = 9
				tushare.ElgCreate(elg)
				fmt.Printf("%s %d %d %d %f\n", v.TsCode, elgvol.Buy, elgvol.Sell, elgvol.Jf, elgvol.Cf)
			}
		}

	}
}

func (ev ElgVol) month(args []string) {
	stocks := model.StockList()

	for _, v := range stocks {
		elgvol := tushare.ElgVolByDate(v.TsCode, args[1], args[2])
		if elgvol.Buy >= 6000000 {
			elg := tushare.Elg{}
			elg.TsCode = v.TsCode
			elg.TradeDateFrom = args[1]
			elg.TradeDateTo = args[1]
			elg.TotalBuyElgVol = elgvol.Buy
			elg.TotalSellElgVol = elgvol.Sell
			elg.BuySellJf = elgvol.Jf
			elg.SellElgVolProportion = elgvol.Cf
			elg.CreateDate = carbon.Now().ToDateString()
			elg.Days = 31
			tushare.ElgCreate(elg)
			fmt.Printf("%s %d %d %d %f\n", v.TsCode, elgvol.Buy, elgvol.Sell, elgvol.Jf, elgvol.Cf)
		}
	}
}

func (ev ElgVol) elgVol(args []string) {
	limit, _ := strconv.ParseInt(args[2], 10, 64)
	stocks := model.StockList()
	for _, v := range stocks {
		elgvol := tushare.ElgVol(v.TsCode, args[1], limit)
		fmt.Println(elgvol.Buy)
		elg := tushare.Elg{}
		elg.TsCode = v.TsCode
		elg.TradeDateFrom = args[1]
		elg.TradeDateTo = args[1]
		elg.TotalBuyElgVol = elgvol.Buy
		elg.TotalSellElgVol = elgvol.Sell
		elg.BuySellJf = elgvol.Jf
		elg.SellElgVolProportion = elgvol.Cf
		elg.CreateDate = carbon.Now().ToDateString()
		elg.Days, _ = strconv.ParseInt(args[2], 10, 64)
		tushare.ElgCreate(elg)
	}
}
